<?php
namespace app\index\controller\Log;
use think\Db;
/**
 * 谭
 * Log\InsertLog::insert("consume",["before_balance"=>"999"]);
 * 日志
 */
class InsertLog{
    static $type=[
        "consume"=>Consume::class,#消费日志
        "enjoy"=>Enjoy::class,
        "pass"=>UpPassword::class,
        "article"=>Article::class,#文章
        "addmoney"=>AddMoney::class,
        "log"=>Log::class,
    ];
    private static $LogObject=[];
    /**
     * 写入日志
     * @param type $type
     * @param array $data  data
     */
    static public function insert($type,$array){
        if(! array_key_exists($type, self::$type)){
           return false;
        }
        if(! array_key_exists($type, self::$LogObject) ){
            self::$LogObject[$type] = new self::$type[$type];
        }
        return self::Construct(self::$LogObject[$type],$array);
        
    }
    /**
     * 
     * @param \app\index\controller\Log\TableLog $objcet 必须继承该接口
     * @param array $array   写入的数据
     */
    static private function Construct(TableLog $objcet,$array){
        $Carr= array_fill_keys( $objcet->Column(), "");
        $data=array_intersect_key($array,$Carr);
        if(empty($data)){
            return false;
        }
       
       return Db::table($objcet->GetTableName())->insert($data);
    }

}
